package com.work.demo.file;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import lombok.Cleanup;

import java.io.*;


public class InputStreamToFile {
	/**
	 * 将InputStream写入本地文件
	 *
	 * @param destination 写入本地目录
	 * @param input       输入流
	 * @throws IOException IOException
	 */
	public static void writeToLocal(String destination, InputStream input)
					throws IOException {
		int index;
		byte[] bytes = new byte[1024];
		FileOutputStream downloadFile = new FileOutputStream(destination);
		while ((index = input.read(bytes)) != -1) {
			downloadFile.write(bytes, 0, index);
			downloadFile.flush();
		}
		input.close();
		downloadFile.close();

	}

	public static void main(String[] args) throws IOException {
		String path = "D://临崖.txt";
		String newPath = "D://临崖232.txt";
		File file = FileUtil.file(path);
		@Cleanup FileInputStream fileInputStream = new FileInputStream(file);
		InputStreamToFile.writeToLocal(newPath, fileInputStream);
		Snowflake snowflake = IdUtil.getSnowflake(1, 1);
		System.out.println(snowflake);
		long l = snowflake.nextId();
		System.out.println(l);
	}
}